﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_Herd
    {
        public bool CreateOneHerd(Herd_Info objHerd_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Herd_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"HerdID", objHerd_Info.HerdID));
            command.Parameters.Add(new SqlParameter(@"HerdName", objHerd_Info.HerdName));
            command.Parameters.Add(new SqlParameter(@"FieldID", objHerd_Info.FieldID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneHerd(Herd_Info objHerd_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Herd_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@HerdID", objHerd_Info.HerdID));
            command.Parameters.Add(new SqlParameter("@HerdName", objHerd_Info.HerdName));
            command.Parameters.Add(new SqlParameter("@FieldID", objHerd_Info.FieldID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneHerd(int HerdID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Herd_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"HerdID", HerdID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllHerd()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Herd_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public Herd_Info GetOneHerd(int HerdID)
        {
            Herd_Info objHerd_Info = new Herd_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Herd_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"HerdID", HerdID));
            try
            {
                objConnection.Open();
                SqlDataReader ordHerd = command.ExecuteReader(); //OjectRecordDatareader
                ordHerd.Read();
                if (ordHerd.HasRows)
                {
                    if (ordHerd["HerdID"] != DBNull.Value)
                    {
                        objHerd_Info.HerdID = Convert.ToInt16(ordHerd["HerdID"]);
                    }
                    if (ordHerd["HerdName"] != DBNull.Value)
                    {
                        objHerd_Info.HerdName = Convert.ToString(ordHerd["HerdName"]);
                    }
                    if (ordHerd["FieldID"] != DBNull.Value)
                    {
                        objHerd_Info.FieldID = Convert.ToInt16(ordHerd["FieldID"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objHerd_Info;
        }
    }
}
